package com.doudoukeji.service.Activity.order.orderdetail;

import android.content.Intent;
import android.view.KeyEvent;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.doudoukeji.service.Activity.ConstantValue;
import com.doudoukeji.service.Dialog.CallPhoneDialog;
import com.doudoukeji.service.R;
import com.sanhai.android.mvp.EduActivity;
import com.sanhai.android.util.TimeUitl;

import org.apache.commons.lang3.StringUtils;

/**
 * @version 2.0
 * @类描述： 订单详情
 * @项目名称：ddgh
 * @类名称： OrderDetailsActivity
 * @包名称： com.doudoukeji.service.Activity.order
 * @创建人：张强 boy
 * @创建时间：2016/3/9 14:20
 * @修改人：
 * @修改时间：2016/3/9 14:20
 * @修改备注： new parent class(用一句话描述修改了什么)
 */

public class OrderDetailsActivity extends EduActivity implements OrderDetailsView {
    // 头布局
    private TextView mTvTitle;

    // 当前界面
    private TextView mTvTime, mTvHospital, mTvDate, mTvDes,
            mTvReward, mTvserviceproject;
    private Button mBtngrab, mBtnBack;
    private LinearLayout mLayCall, mLlRob, mLlRobOk, mLlRobNo;
    private String orderID;

    //数据
    private OrderDetailsBean detailsBean = new OrderDetailsBean();
    private OrderDetailsPresenter presenter;

    @Override
    public void setContentLayout() {
        setContentView(R.layout.activity_myorderdetails);
    }

    @Override
    public void init() {
        // 实例化topbar
        initTopbar();
        // 当前界面
        initView();
    }

    @Override
    public void onClickEvent(View view) {
        switch (view.getId()) {
            case R.id.lay_call:
                if (StringUtils.isNotBlank(detailsBean.getPhone())) {
                    CallPhoneDialog dialog = new CallPhoneDialog(
                            OrderDetailsActivity.this, detailsBean.getPhone());
                    dialog.show();
                }
                break;
            case R.id.btn_grab:
                presenter.grabSingle(detailsBean.getId());
                break;
            case R.id.btn_back:
                finish();
                break;
            case R.id.btn_fail_ok:
                Intent intentfail = new Intent();
                intentfail.setAction(ConstantValue.Main_CHANGE);
                intentfail.putExtra("position", "0");
                OrderDetailsActivity.this.sendBroadcast(intentfail);
                this.finish();
                break;
            case R.id.btn_succed_ok:
                Intent intentred = new Intent();
                intentred.setAction(ConstantValue.Main_CHANGE);
                intentred.putExtra("position", "1");
                OrderDetailsActivity.this.sendBroadcast(intentred);
                this.finish();
                break;
            default:
                break;
        }
    }


    // 实例化topbar
    private void initTopbar() {
        mTvTitle = (TextView) findViewById(R.id.tv_title);
        mTvTitle.setText("订单详情");

    }

    // 实例化当前界面
    public void initView() {
        orderID = getIntent().getStringExtra("orderid");

        // 时间
        mTvTime = (TextView) findViewById(R.id.tv_time);

        // 医院
        mTvHospital = (TextView) findViewById(R.id.tv_hospital);

        //服务项目
        mTvserviceproject = (TextView) findViewById(R.id.tv_serviceproject);


        // 日期
        mTvDate = (TextView) findViewById(R.id.tv_date);


        // 悬赏金额
        mTvReward = (TextView) findViewById(R.id.tv_reward_money);

        //需求备注
        mTvDes = (TextView) findViewById(R.id.tv_order_needdescribe);

        //主页面
        mLlRob = (LinearLayout) findViewById(R.id.ll_rob);
        //抢单成功
        mLlRobOk = (LinearLayout) findViewById(R.id.ll_rob_succeed);
        //抢单失败
        mLlRobNo = (LinearLayout) findViewById(R.id.ll_rob_defeated);
        mBtnBack = (Button) findViewById(R.id.btn_back);
        mBtnBack.setOnClickListener(this);
        mBtngrab = (Button) findViewById(R.id.btn_grab);
        mBtngrab.setOnClickListener(this);
        mLayCall = (LinearLayout) findViewById(R.id.lay_call);
        mLayCall.setOnClickListener(this);
        findViewById(R.id.btn_fail_ok).setOnClickListener(this);
        findViewById(R.id.btn_succed_ok).setOnClickListener(this);


        presenter = new OrderDetailsPresenter(this, this);
        presenter.getOrderDetails(orderID);
    }


    @Override
    public void getOrderDetaiul(OrderDetailsBean detailsBean) {
        if (detailsBean != null) {
            this.detailsBean = detailsBean;
            mTvTime.setText(TimeUitl.getDayStr(detailsBean.getCreatedata()));
            mTvHospital.setText(detailsBean.getHospital());
            mTvserviceproject.setText(detailsBean.getService());
            mTvDate.setText(detailsBean.getOrderDate());
            mTvReward.setText("￥" + detailsBean.getMoney());
            mTvDes.setText(detailsBean.getDescr());
        }
    }

    @Override
    public void getfail() {
        showToastMessage("加载失败");
        mLlRob.setVisibility(View.VISIBLE);
        mBtngrab.setVisibility(View.VISIBLE);
        mLlRobOk.setVisibility(View.GONE);
        mLlRobNo.setVisibility(View.GONE);
    }

    @Override
    public void grabSucced() {
        mBtngrab.setVisibility(View.GONE);
        mLlRob.setVisibility(View.GONE);
        mLlRobOk.setVisibility(View.VISIBLE);
        mLlRobNo.setVisibility(View.GONE);
    }

    @Override
    public void grabFail() {
        mBtngrab.setVisibility(View.GONE);
        mLlRob.setVisibility(View.GONE);
        mLlRobOk.setVisibility(View.GONE);
        mLlRobNo.setVisibility(View.VISIBLE);
    }

    /**
     * 监听返回键
     *
     * @param keyCode
     * @param event
     * @return
     */
    public boolean onKeyDown(int keyCode, KeyEvent event) {

        if (keyCode == KeyEvent.KEYCODE_BACK
                && event.getRepeatCount() == 0) {
            finish();
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }
}
